﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MoveCtrl2D
{
	protected RoleCtrl m_MainCtrl;
	protected float horizenInput;
	protected float verticalInput;

	public Rigidbody2D Rb2d { get; protected set; }
	public Collider2D Collider { get; protected set; }
	public Vector2 VelocityDir { get; protected set; }
	public Vector2 Velocity { get; protected set; }

	public float VelocityMultiply { get; protected set; } = 1;
	public MoveCtrl2D(RoleCtrl ctrl,Rigidbody2D rb2d)
	{
		this.m_MainCtrl = ctrl;
		this.Rb2d = rb2d;
		this.Collider = ctrl.gameObject.GetComponent<Collider2D>();
	}

	virtual public void Update()
	{
		horizenInput = Input.GetAxis("Horizontal");
		verticalInput = Input.GetAxis("Vertical");

		//圆形空间下的坐标
		float s_h = horizenInput * Mathf.Sqrt(1 - verticalInput * verticalInput / 2.0f);
		float s_v = verticalInput * Mathf.Sqrt(1 - horizenInput * horizenInput / 2.0f);
		VelocityDir = new Vector2(s_h, s_v);
	}
	virtual public void FixedUpdate()
	{
		if (VelocityDir != Vector2.zero)
		{
			Velocity = VelocityDir * m_MainCtrl.RoleEntity.CurrentSpeed;
		}
		else
		{
			Velocity = new Vector2(Mathf.Lerp(Velocity.x, 0, 0.3f), Mathf.Lerp(Velocity.y, 0, 0.3f));
		}
		Velocity *= VelocityMultiply;
		this.Rb2d.velocity = Velocity;
	}

	public void StopMove()
	{
		VelocityDir = Vector2.zero;
		//this.Rb2d.velocity = Vector2.zero;
	}

	public void SetVelocityDir(Vector2 dir)
	{
		this.VelocityDir = dir;
	}
	
	public void SetVelocityMultiply(float _multiply)
	{
		if (_multiply < 0)
			return;
		VelocityMultiply = _multiply;
	}
}
